<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-title">管理员登录</div>
      <el-form status-icon :model="loginForm" :rules="rules" ref="ruleForm" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user-solid"
            placeholder="用户名"
            @keyup.enter.native="login" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont el-icon-mymima"
            show-password
            placeholder="密码"
            @keyup.enter.native="login" />
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="login">登录</el-button>
    </div>
  </div>
</template>

<script>
import { generaMenu } from '@/assets/js/menu'
export default {
  data: function () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    login() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          const that = this
          let param = new URLSearchParams()
          param.append('username', that.loginForm.username)
          param.append('password', that.loginForm.password)
          that.axios.post('/api/users/login', param).then(({ data }) => {
            if (data.flag) {
              that.$store.commit('login', data.data)
              generaMenu()
              that.$message.success('登录成功')
              that.$router.push({ path: '/' })
            } else {
              that.$message.error(data.message)
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  background: url(https://minio.gysy.ltd/aurora/photos/20c910eedb24fb4ec21a03ec25d5e281.jpg) center center / cover
  no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-card {
  background: rgba(255, 255, 255, 0.8);  /* 半透明背景 */
  padding: 40px 60px;
  width: 350px;
  max-height: 80vh;
  overflow-y: auto;
  border-radius: 8px;  /* 可选：添加圆角效果 */
}

.login-title {
  color: #303133;
  font-weight: bold;
  font-size: 1.2rem;  /* 适当调整字体大小 */
}

.login-form {
  margin-top: 1.2rem;
}

.login-card button {
  margin: 1rem auto 0;  /* 使用 margin: auto 让按钮居中 */
  height: 36px;
  font-size: 0.9rem;
  padding: 0 20px;
  display: block;  /* 确保按钮是块级元素 */
  max-width: 200px; /* 如果需要限制按钮宽度 */
}
</style>
